const { get, post } = require('../../request/index')
const { typeJson } = require('../../utils/typejson')

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    net: {
      type: Object,
      value: {
        url: '',
        method: '',
        key: '',
        value: '',
        data:{}
      },
      observer: function(newVal, oldVal, changedPath) {
        this.getList(newVal)
      }
    },
    value: {
      type: String,
      value: null
    },

  },

  /**
   * 组件的初始数据
   */
  data: {
    show: false,
    display: '',

    list: [],
    listValue: [],
    backuplistValue: [],
    backup: [],
    result: [],
    resultint: [],
    resultVal: [],

    data: [],
    
    value: '',
    search: ''
  },

  /**
   * 组件的方法列表
   */
  methods: {


    async getList(net) {
      const { url, method, key, value,data } = net
      if (data.mineId == true) {
        let prolist =  wx.getStorageSync('prolist')==''?wx.getStorageSync('prolist'):JSON.parse(wx.getStorageSync('prolist'))
        data.mineId = prolist.mineId
      }
      var res = ""
      if(method=='get') {
        res = await get(url, data)
      } else {
        res = await post(url, data)
      }
    
      let { list } = res.data
      var tempList = []
      var tempResult = []
      var tempListValue = []

      var split = []
    
      if(this.properties.value && this.properties.value.indexOf(',')>0) {
        split = this.properties.value.split(',')
      } else if (this.properties.value) {
        split.push(this.properties.value)
      }

    

      list.map(x=>{
        tempList.push(x[value])
        tempListValue.push(x[key])
        split.map(y=>{
          if(x[key]==y) {
            tempResult.push(x[value])
          }
        })
      })

      



      let display = ''
      tempResult.map((x,y)=>{
        display += y==tempResult.length-1 ? x : (x+',') 
      })

      this.setData({
        list: tempList,
        backupListValue: tempListValue,
        listValue: tempListValue,
        backup: tempList,
        result: tempResult,
        data: list,
        display: display
      })

      
    },

    showPopup() {
      this.setData({ show: true })
    },
  
    onClose() {
      this.setData({ show: false })
  
      var tempa = ""
      this.data.result.map(x=>{
        tempa += x
        tempa += "," 
      })
      tempa = tempa.substring(0,tempa.length - 1)
      
      this.setData({
        display: tempa
      })

      var tempb = ""
      this.data.resultint.map(x=>{
        tempb += this.data.listValue[x]
        tempb += ","
      })
      tempb = tempb.substring(0,tempb.length - 1)
      this.triggerEvent('change', tempb)
      console.log(tempb);
    },

    bindtap() {
      this.setData({ show: true })
    },


    onChange(event) {
  
      let result = event.detail
      this.setData({
        result: result
      });
      const { data } = this.data
      const { key, value } = this.data.net
      let temp = []
      data.map(x => {
        result.map(y => {
           if(x[value] == y) {
             temp.push(x[key])
           }
        })
      })

      let a = ''
      temp.map((x,y)=>{
        a += y==temp.length-1 ? x : (x+',') 
      })
      this.triggerEvent('change', a)
      console.log(a);
      
      let display = ''
      result.map((x,y)=>{
        display += y==result.length-1 ? x : (x+',') 
      })
      this.setData({
        display: display
      });

    },
  
    toggle(e) {
      const { index } = event.currentTarget.dataset
      const checkbox = this.selectComponent(`.checkboxes-${index}`)
      checkbox.toggle();
    },

    click(e) {
      let {index} = e.target.dataset;
      var a = this.data.result
      var b = this.data.resultint
      var temp = []
      var lock = false
      b.map(x=>{
        if (x==index) {
          lock = true
        }
      })
      if (lock) { return }
      b.push(index)

      b.map(x=>{
        temp.push(this.data.list[x])
      })

      this.setData({
        result: temp,
        resultint: b
      })
  
    
    },

    pClick(e) {

      
      let { index } = e.currentTarget.dataset;
      var a = this.data.result
      var b = this.data.resultint


      a.splice(index,1)
      b.splice(index,1)

      this.setData({
        resultint: b
      })

      this.setData({
        result: a
      })
    },

    editingChanged(e) {
        let { backup, backupListValue } = this.data
        let temp = []
        let tempList = []
        backup.map((x, index) => {
          if (x.indexOf(e.detail)>-1) {
            temp.push(x)
            tempList.push(backupListValue[index])
          }
        })
        this.setData({
          list: temp,
          listValue: tempList
        })
      
    }
  
   
  }

})
